<?php
if ($act != "" && $detail == "" || $detail == "all") {
    $match_result = Frontend::findMatchResultTimeStamp((int) $_today)->count();
} else {
    $tour_url = Frontend::findTournamentByUrl($detail);
    foreach ($tour_url as $_tour_url) {
        $match_result = Frontend::findMatchResultByTourIdTimeStamp((int)$_tour_url["_id"],(int)$_today)->count();
    }
}
if ($page == NULL) {
    $_page = 1;
} else {
    $_page = $page;
}

$numPage = ceil($match_result / $num_page);
$showeachside = 5;
$eitherside = ($showeachside * 1);

if (empty($page))
    $start = 0;
else
    $start = $page;

$pageCurrent = $num_page * ($_page - 1);

if ($act != "" && $detail == "" || $detail == "all") {
    $url = "";
    $match_all = Frontend::findMatchResultTimeStamp((int) $_today, "desc")->skip($pageCurrent)->limit($num_page);
} else {
    $url = $detail . "/";
    $match_all = Frontend::findMatchResultByTourIdTimeStamp((int)$_tour_url["_id"], (int) $_today, "desc")->skip($pageCurrent)->limit($num_page);
}
$arr_match = array();
$arr = iterator_to_array($match_all);

foreach ($match_all as $keys => $_match_all) {
    $datematch = explode(" ", $_match_all["datematch"]);
    $arr_match[$datematch[0]][] = $arr[$keys];
}
?>
<div class="row" id="main">
    <div class="span14">
        <div class="span3">
            <ul class="nav nav-list nav-list-vivid">
                <li class="nav-header">Giải đấu</li>
                <li><a href="<?php echo SKIN_FPATH . "ketqua/all" ?>">Tất cả</a></li>
                <?php
                $country = Frontend::findAllCountry();
                foreach ($country as $_country) {
                    ?>
                    <li class="nav-header"><?php echo $_country["name"] ?></li>
                    <?php
                    $tournament = Frontend::findTournamentByCountry((int) $_country["_id"]);
                    foreach ($tournament as $_tournament) {
                        $str = trim(Core::str_process($_tournament["title"]));
                        $link = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $str));

                        $c_match = Frontend::findMatchResultByTourIdTimeStamp((int) $_tournament["_id"], (int)$_today);
                        ?>
                        <li class="children">
                            <a href="<?php echo SKIN_FPATH . "ketqua/" . $link; ?>">
                                <img src="/skin/frontend/caulacbo/oYVHSYPZZ4.png" class="logo" width="20">
                                <?php echo $_tournament["title"]; ?>                
                                <span class="label label-inverse pull-right"><?php echo count(iterator_to_array($c_match)); ?></span>
                            </a>
                        </li>
                    <?php } ?>
                <?php } ?>
            </ul>
        </div>
        <div class="btn-group pull-right">
                    <!--<a class="btn" href="#"><?php //echo $match_timestamp       ?> kết quả</a>-->
            <?php if ($page > 1) { ?>
                <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . 1 ?>"><< First</a>
                <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . ($_page - 1) ?>">Prev</a>
            <?php } else { ?>
                <a class="btn disabled" href="javascript:void(0)"><< First</a>
                <a class="btn disabled" href="javascript:void(0)">Prev</a>
            <?php } ?>    
            <?php
            for ($i = 1; $i <= $numPage; $i++) {
                if (($i > ($start - $eitherside)) && ($i < ($start + $eitherside))) {
                    ?>
                    <a class="btn <?php
                    if ($i == $_page) {
                        echo "active";
                    }
                    ?>" href="<?php echo SKIN_FPATH . "ketqua/" . $url . $i ?>"><?php echo $i ?></a>
                       <?php
                   }
               }
               ?>
               <?php if ($_page < $numPage) { ?>
                <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . ($_page + 1) ?>">Next</a>
                <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . $numPage ?>">Last >></a>
            <?php } else { ?>
                <a class="btn disabled" href="javascript:void(0)">Next</a>
                <a class="btn disabled" href="javascript:void(0)">Last >></a>
            <?php } ?>
        </div>
        <div class="span9">
            <table class="table table-bordered table-events">
                <?php
                foreach ($arr_match as $key => $_arr_match) :
                    $date = date('l, Y-m-d', strtotime($key));
                    ?>
                    <thead>
                        <tr>
                            <th colspan="10">
                                <img src="/skin/frontend/icon/Flaticon_4430.png">
                                <?php echo $date; ?>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="head">
                            <td class="time">Giờ</td>
                            <td class="p">Trận</td>
                            <td>Toàn trận</td>
                            <td>Hiệp 1</td>
                            <td>Hiệp phụ</td>
                            <td>Luân lưu</td>
                            <td></td>
                        </tr>
                        <?php
                        // Tìm Match theo ngày
                        foreach ($_arr_match as $_match_date) :
                            $team1 = Frontend::findTeamById((int) $_match_date["team1"]);
                            foreach ($team1 as $_team1) :
                                $t1 = $_team1["name"];
                            endforeach;
                            $team2 = Frontend::findTeamById((int) $_match_date["team2"]);
                            foreach ($team2 as $_team2) :
                                $t2 = $_team2["name"];
                            endforeach;

                            $str_team = trim(Core::str_process($t1 . " " . $t2));
                            $link_team = strtolower(preg_replace('/[^a-z0-9]+/i', '-', $str_team));
                            

                            $odd = Frontend::findOddByMacth((int) $_match_date["_id"]);
                            $match_dt = Frontend::findMatchById((int) $_match_date["matchid"]);
                            foreach ($match_dt as $_match_dt){
                                $match_id = $_match_dt["_id"];
                                $tour_dt = Frontend::findTournamentById((int)$_match_dt["TourID"]);
                            }
                            //if (iterator_to_array($odd)):
                            $shot1 = $_match_date["team1r1"] + $_match_date["team1r2"] + $_match_date["team1r3"] + $_match_date["team1r4"];
                            $shot2 = $_match_date["team2r1"] + $_match_date["team2r2"] + $_match_date["team2r3"] + $_match_date["team2r4"];
                            ?>
                            <tr class="odd">
                                <td rowspan="2" class="time">
                                    <img src="/skin/frontend/icon/Flaticon_4076.png"><br>
                                    <?php echo $_match_date["starttime"] ?>               
                                </td>
                                <td class="p <?php if ($shot1 > $shot2) { ?> winner<?php } ?>">
                                    <?php echo $t1; ?>                
                                </td>

                                <td class="<?php if ($shot1 > $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team1r1"] ?> </td>
                                <td class="<?php if ($shot1 > $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team1r2"] ?> </td>
                                <td class="<?php if ($shot1 > $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team1r3"] ?> </td>
                                <td class="<?php if ($shot1 > $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team1r4"] ?> </td>
                                <td rowspan="2" class="go">
                                    <?php foreach ($tour_dt as $_link_match) { ?>
                                        <a href="<?php echo SKIN_FPATH . "ketqua/" . $_link_match["location"] . "/" . $link_team . "/" . $match_id ?>">
                                            <img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/Flaticon_1413.png" />
                                        </a>
                                    <?php } ?>
                                </td>
                            </tr>
                            <tr class="odd">
                                <td class="p <?php if ($shot1 < $shot2) { ?> winner<?php } ?>"><?php echo $t2; ?></td>
                                <td class="<?php if ($shot1 < $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team2r1"] ?></td>
                                <td class="<?php if ($shot1 < $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team2r2"] ?></td>
                                <td class="<?php if ($shot1 < $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team2r3"] ?></td>
                                <td class="<?php if ($shot1 < $shot2) { ?> winner<?php } ?>"><?php echo $_match_date["team2r4"] ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <div class="btn-group pull-right">
                    <!--<a class="btn" href="#"><?php //echo $match_timestamp       ?> kết quả</a>-->
                <?php if ($page > 1) { ?>
                    <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . 1 ?>"><< First</a>
                    <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . ($_page - 1) ?>">Prev</a>
                <?php } else { ?>
                    <a class="btn disabled" href="javascript:void(0)"><< First</a>
                    <a class="btn disabled" href="javascript:void(0)">Prev</a>
                <?php } ?>    
                <?php
                for ($i = 1; $i <= $numPage; $i++) {
                    if (($i > ($start - $eitherside)) && ($i < ($start + $eitherside))) {
                        ?>
                        <a class="btn <?php
                        if ($i == $_page) {
                            echo "active";
                        }
                        ?>" href="<?php echo SKIN_FPATH . "ketqua/" . $url . $i ?>"><?php echo $i ?></a>
                           <?php
                       }
                   }
                   ?>
                   <?php if ($_page < $numPage) { ?>
                    <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . ($_page + 1) ?>">Next</a>
                    <a class="btn" href="<?php echo SKIN_FPATH . "ketqua/" . $url . $numPage ?>">Last >></a>
                <?php } else { ?>
                    <a class="btn disabled" href="javascript:void(0)">Next</a>
                    <a class="btn disabled" href="javascript:void(0)">Last >></a>
                <?php } ?>
            </div>

            <div style="margin-top: 18px;" class="clearfix">
            </div>
        </div>            
    </div>
</div>